package com.example.springboot2022nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot2022nocv.entity.ChinaTotal;
import com.example.springboot2022nocv.entity.LineTrend;
import com.example.springboot2022nocv.entity.NocvData;
import com.example.springboot2022nocv.entity.NocvNews;
import com.example.springboot2022nocv.service.ChinaTotalService;
import com.example.springboot2022nocv.service.IndexService;
import com.example.springboot2022nocv.service.NocvNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;
    @Autowired
    private ChinaTotalService chinaTotalService;
    @Autowired
    private NocvNewsService nocvNewsService;


    @RequestMapping("/")
    public String index(Model model) throws ParseException {

        //1.找到ID最大的那一条数据
        Integer id = chinaTotalService.maxID();
        //2.根据ID进行查找数据
        ChinaTotal chinaTotal = chinaTotalService.getById(id);
        model.addAttribute(  "chinaTotal",chinaTotal);
        model.addAttribute( "chinaTotal", chinaTotal);
        return "index";
    }

    @RequestMapping("/toChina")
    public String toChina(Model model) throws ParseException {
        //找到ID最大的那一条数据
        Integer id = chinaTotalService.maxID();
        //2.根据ID进行查找数据
        ChinaTotal chinaTotal = chinaTotalService.getById(id);
        model.addAttribute("chinaTotal",chinaTotal);
        //3.疫情播报新闻
        List<NocvNews> newsList = nocvNewsService.listNewsLimit5();
        model.addAttribute("newsList",newsList);
        return "china";
    }


    //查询全国疫情数据
    @RequestMapping("/query")
    @ResponseBody
    public List<NocvData> queryData(){
        List<NocvData> list = indexService.list();
        return list;
    }



    //转到饼状图
    @RequestMapping("/toPie")
    public  String toPie(){
        return "pie";
    }

    //查询饼状图数据
    @RequestMapping("/queryPie")
    @ResponseBody
    public List<NocvData> queryPieData(){
        List<NocvData> list = indexService.list();
        return list;
    }

    //转到树状图
    @RequestMapping("/toBar")
    public  String toBar(){
        return "bar";
    }

    //查询树状图数据
    @RequestMapping("/queryBar")
    @ResponseBody
    public Map<String,List<Object>> queryBarData(){
        //所有城市数据：数值
        List<NocvData> list = indexService.list();
        //所有城市名称数据
        List<String> cityList = new ArrayList<>();
        for(NocvData data : list){
            cityList.add(data.getName());
        }
        //所有城市感染人数数值
        List<Integer> dataList = new ArrayList<>();
        for (NocvData data : list){
            dataList.add(data.getValue());
        }
        Map map = new HashMap();
        map.put("cityList",cityList);
        map.put("dataList",dataList);
        return map;
    }

    @RequestMapping("/toLine")
    public  String toLine(){
        return "line";
    }


    @RequestMapping("/queryLine")
    @ResponseBody
    public Map<String,List<Object>> queryLineData(){

        //查询近七天的所有数据
        List<LineTrend> list7Day =  indexService.findSevenData();
        //封装所有确诊人数
        List<Integer> confirmList = new ArrayList<>();
        //封装所有的隔离人数
        List<Integer> isolationList = new ArrayList<>();
        //封装所有的治愈人数
        List<Integer> cureList = new ArrayList<>();
        //封装所有的死亡人数
        List<Integer> deadList = new ArrayList<>();
        //封装所有的疑似人数
        List<Integer> similarList = new ArrayList<>();

        for (LineTrend data : list7Day) {

            confirmList.add(data.getConfirm());
            isolationList.add(data.getIsolation());
            cureList.add(data.getCure());
            deadList.add(data.getDead());
            similarList.add(data.getSimilar());
            System.out.println(data.getIsolation());
        }

        Map map = new HashMap();
        map.put("confirmList",confirmList);
        map.put("isolationList",isolationList);
        map.put("cureList",cureList);
        map.put("deadList",deadList);
        map.put("similarList",similarList);

        return map;
    }


}
